package regulator;

public class Test {

}
/*
 * import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
	static class Node implements Comparable<Node> {
		int to;
		int w;
		Node(int x, int y){
			this.to = x;
			this.w = y;
		}
		@Override
		public int compareTo(Node o) {
			// TODO Auto-generated method stub
			return this.w - o.w;
		}
		
		
	}
	

//	static Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
//  static InputReader sc = new InputReader(System.in);
	static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    
//    static Stack<Character> stack = new Stack<Character>();
//    static LinkedList<Pos> queue = new LinkedList<Pos>();
//    static int dir[][] = {{0,1},{0,-1},{1,0},{-1,0}}	s
    static ArrayList<Integer> list = new ArrayList<Integer>();
    static int n,t,ans = Integer.MAX_VALUE;
    static boolean vis[];	
//  static ArrayList<Node> data[];
    static int data[][];
	public static void main(String[] args) throws NumberFormatException, IOException {
		
		n = nextInt();
		t = nextInt();
		data = new int[n+1][n+1];
		while(t-->0) {
			int x = nextInt();
			int y = nextInt();
			int w = nextInt();
			data[x][y] = w;
			data[y][x] = w;
		}
		t = nextInt();
		boolean have = false;
		while(t-->0) {
			int k = nextInt();
			vis = new boolean[n+1];
			int pos[] = new int [k+2];
			boolean flag = true;
			int sum = 0;
			for(int i = 1; i <= k; i++) {
				pos[i] = nextInt();
				if(vis[pos[i]] == true) {
					flag = false;
				}	
				vis[pos[i]] = true;
			}
			for(int i = 1; i <= n; i++) {
				if(!vis[i]) {
					flag = false;
					break;
				}
			}
			
			if(!flag) continue;
			
			for(int i = 1; i <= k+1; i++) {
				if(data[pos[i-1]][pos[i]] > 0) {
					sum += data[pos[i-1]][pos[i]];
				}
				else {
					flag = false;
					break;
				}
			}
			
			if(flag) {
				ans = Math.min(ans, sum);
				have = true;
			}	
		}
		
		if(have)
			System.out.println(ans);
		else
			System.out.println(-1);
		out.flush();
	}


	


	static int nextInt() throws IOException {
		in.nextToken();
		return (int)in.nval;
	}
	static double nextDouble() throws IOException {
		in.nextToken();
		return in.nval;
	}
	static long nextLong() throws IOException {
		in.nextToken();
		return (long)in.nval;
	}
}	

class InputReader{
    StringTokenizer tokenizer;
    BufferedReader reader;
    public InputReader(InputStream stream) {
        super();
        reader = new BufferedReader(new InputStreamReader(stream));
    }
    public String next() throws IOException {
        while(tokenizer == null || !tokenizer.hasMoreTokens()) {
            tokenizer = new StringTokenizer(reader.readLine());
        }
        return tokenizer.nextToken();
    }
    public int nextInt() throws NumberFormatException, IOException {
        return Integer.parseInt(next());
    }
    public short nextShort() throws NumberFormatException, IOException {
        return Short.parseShort(next());
    }
    public long nextLong() throws NumberFormatException, IOException {
        return Long.parseLong(next());
    }
    public double nextDouble() throws NumberFormatException, IOException {
        return Double.parseDouble(next());
    }
    public String nextLine() throws IOException{
        String line = "";
        if(tokenizer.hasMoreTokens())
            line = tokenizer.nextToken();
        else
            try {
                return reader.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
        while(tokenizer.hasMoreTokens())
            line += " " + tokenizer.nextToken();
        return line;
    }
    public boolean hasNext() {
        if (tokenizer != null && tokenizer.hasMoreTokens())
            return true;
        try {
            tokenizer = new StringTokenizer(reader.readLine());
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}

 */
